<?php
declare(strict_types=1);

namespace app\api\controller\channel;

use app\common\controller\Api;
use app\common\model\delivery\Channel;
use app\common\model\delivery\ChannelGird;
use app\common\model\delivery\ChannelOrder;
use app\common\model\delivery\Customer;
use app\common\model\delivery\Driver;
use think\annotation\route\Get;
use think\annotation\route\Group;
use think\facade\Db;

#[Group("channel/index")]
class Index extends Api
{

    protected function _initialize()
    {
        parent::_initialize();
        if($this->auth->id!=1 && $this->auth->id!=6 && $this->auth->id!=10){
            $this->error('没有权限');
        }
    }

    #[Get('list')]
    public function list()
    {
        $list=Channel::select();
        $this->success('',$list);
    }

    #[Get('tongji')]
    public function tongji()
    {
        $channel_id=$this->request->get('channel_id');
        $channel=Channel::find($channel_id);
        $shouyi=[
            'all_money'=>0,
            'today_money'=>0
        ];
        $wuliu=[
            'total'=>0,
            'receive'=>0
        ];
        $prefix=getDbPrefix();
        $gird=ChannelGird::where(['channel_id'=>$channel->id])->count();
        $driver=Driver::where(['channel_id'=>$channel->id])->count();
        $shouyi['all_money']=ChannelOrder::where(['channel_id'=>$channel->id,'is_cancel'=>0])->sum('total_money');
        $start=strtotime(date('Y-m-d 00:00:00'));
        $end=strtotime(date('Y-m-d 23:59:59'));
        $shouyi['today_money']=ChannelOrder::where(['channel_id'=>$channel->id,'is_cancel'=>0])->whereBetween('createtime',[$start,$end])->sum('total_money');
        $wuliu['total']=ChannelOrder::where(['channel_id'=>$channel->id,'is_cancel'=>0,'delivery_day'=>date('Y-m-d')])->count();
        $wuliu['receive']=ChannelOrder::where(['channel_id'=>$channel->id,'is_cancel'=>0,'delivery_day'=>date('Y-m-d')])->whereNotNull('receive_time')->count();
        $sql="
            SELECT 
            coupon.type,count(1) as count,
            SUM(list.balance) as balance,
            SUM(list.total-list.take) as total 
            FROM {$prefix}coupon_list list,{$prefix}coupon coupon
            where list.coupon_id=coupon.id and list.total>list.take and list.channel_id={$channel->id}
            GROUP BY coupon.type
        ";
        $couponlist=Db::query($sql);
        $coupon=[];
        foreach ($couponlist as $value){
            $coupon[$value['type']]=$value;
        }
        $store_num=Customer::where(function ($query) use ($channel){
            $query->where('channel_id','=',$channel->id);
        })->count();
        $store_month_num=Customer::where(function ($query) use ($channel){
            $query->where('channel_id','=',$channel->id);
            //当月新增
            $start=strtotime(date('Y-m-01'));
            $query->where('createtime','between',[$start,time()]);
        })->count();
        $store=[
            'store_num'=>$store_num,
            'store_month_num'=>$store_month_num
        ];
        $this->success('',compact('channel','coupon','gird','driver','store','shouyi','wuliu'));
    }
}